﻿using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace Trade.Journal
{
    /// <summary>
    /// NewRecordWindow.xaml 的交互逻辑
    /// </summary>
    public partial class NewRecordWindow : Window
    {
        private Relationship Relationship { get; set; }

        public NewRecordWindow(Relationship relationship = null, string contract = null)
        {
            Relationship = relationship;
            InitializeComponent();

            if (relationship?.RelationType == RelationType.Overweight)
            {
                Title = "加仓";
                Icon = TryFindResource("Icon2") as ImageSource;
            }
            else if (relationship?.RelationType == RelationType.Reverse)
            {
                Title = "反手";
                Icon = TryFindResource("Icon3") as ImageSource;
            }
            else if (relationship?.RelationType == RelationType.Reenter)
            {
                Title = "再次入场";
                Icon = TryFindResource("Icon4") as ImageSource;
            }
            if (contract != null && DataContext is NewRecordWindowViewModel v)
            {
                v.Contract = contract;
                tbContact.IsReadOnly = true;
            }
        }

        public NewRecordWindow(RoadMap map, RelationType relation, string contract)
        {
            InitializeComponent();

            if (relation == RelationType.Overweight)
            {
                Title = "加仓";
                Icon = TryFindResource("Icon2") as ImageSource;
            }
            else if (relation == RelationType.Reverse)
            {
                Title = "反手";
                Icon = TryFindResource("Icon3") as ImageSource;
            }
            else if (relation == RelationType.Reenter)
            {
                Title = "再次入场";
                Icon = TryFindResource("Icon4") as ImageSource;
            }

            if (DataContext is NewRecordWindowViewModel v)
            {
                v.GroupId = map.FirstOrDefault().RecordId;
                if (contract != null)
                {
                    v.Contract = contract;
                    tbContact.IsReadOnly = true;
                }
                v.RoadMap = map;
                map.Add(new RoadMapNode { Content = Title, RecordId = v.RecordId });
            }
        }


        private void Underweight_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v && !(v.DealRecords.LastOrDefault() is CloseDealViewModel))
            {
                v.DealRecords.Add(new UnderweightDealViewModel() { Sequence = v.DealRecords.Any() ? v.DealRecords.Max(x => x.Sequence) + 1 : 2 });
            }
        }

        private void Overweight_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v)
            {
                //var win = new NewRecordWindow(new Relationship { LeftId = (DataContext as NewRecordWindowViewModel)?.RecordId, RelationType = RelationType.Overweight }, v.Contract);
                var win = new NewRecordWindow(v.RoadMap, RelationType.Overweight, v.Contract);
                win.Owner = this;
                win.ShowDialog();
            }
        }

        private void OtherTrade_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v)
            {
                var win = new NewRecordWindow(v.RoadMap, RelationType.Reenter, v.Contract); //new NewRecordWindow(new Relationship { LeftId = (DataContext as NewRecordWindowViewModel)?.RecordId, RelationType = RelationType.Reenter }, v.Contract);
                win.Owner = this;
                win.ShowDialog();
            }
        }

        private void ReverseTrade_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v)
            {
                var win = new NewRecordWindow(v.RoadMap, RelationType.Reverse, v.Contract);
                win.Owner = this;
                win.ShowDialog();
            }
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v && !(v.DealRecords.LastOrDefault() is CloseDealViewModel))
            {
                v.DealRecords.Add(new CloseDealViewModel() { Sequence = v.DealRecords.Any() ? v.DealRecords.Max(x => x.Sequence) + 1 : 2 });
            }
        }

        private void DeleteDeal_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (e.Parameter is DealViewModel dr && DataContext is NewRecordWindowViewModel v)
                v.DealRecords.Remove(dr);
        }


        private void ConfirmedCommand_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v)
            {
                if (DbHelper.SaveRecord(v.Build()))
                {   
                    DataShop.LoadRecord(v.RecordId);
                    DialogResult = true;
                    Close();
                }
            }
        }


        private void ConfirmedCommand_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v)
            {
                e.CanExecute = v.CanSave;//CheckSavable(v);
            }
        }



        private bool CheckSavable(NewRecordWindowViewModel v)
        {
            if (string.IsNullOrWhiteSpace(v.Contract))
                return false;


            return true;
        }

        private void CopyId_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is NewRecordWindowViewModel v)
                Clipboard.SetText(v.RecordId);
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            if (DialogResult != true && DataContext is NewRecordWindowViewModel v)
                v.RoadMap.Remove(v.RoadMap.FirstOrDefault(x => x.RecordId == v.RecordId));

            base.OnClosing(e);
        }
    }
}

